package com.gzy.xuexiziliao.controller;





import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.common.result.Result;
import com.gzy.xuexiziliao.pojo.entity.Doctor;
import com.gzy.xuexiziliao.pojo.entity.Information;
import com.gzy.xuexiziliao.pojo.entity.Student;
import com.gzy.xuexiziliao.service.DoctorService;
import com.gzy.xuexiziliao.service.InformationService;
import com.gzy.xuexiziliao.service.StudentService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 学生控制类
 */
@RestController
@RequestMapping("/student/students")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private InformationService informationService;
    @Autowired
    private DoctorService doctorService;

    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("新增学生,student:{}",student);
        studentService.save(student);
        return Result.success("新增成功");
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除学生,ids:{}",ids);
        studentService.delete(ids);
        return Result.success("删除成功");
    }

    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("更新学生:{}",student);
        studentService.update(student);
        return Result.success("更新成功");
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询学生：{}",id);
        Student student =studentService.getById(id);
        if (student==null){
            return Result.error("未找到该学生");
        }
        return Result.success(student);
    }
    @GetMapping("/name")
    public Result<List<Student>> getByName(@RequestParam String name){
        log.info("根据name查询学生：{}",name);
        List<Student> Student =studentService.getByName(name);
        if (Student == null){
            return Result.error("未找到该学生");
        }
        return Result.success(Student);
    }

    @GetMapping("/list")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult pageBean=studentService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }



    @PutMapping("/status")
    public Result checkStatus(@RequestBody Student student,HttpServletRequest request) {
        // 从request中获取studentId
        Integer studentId = (Integer) request.getAttribute("studentId");
        // 设置studentId到student对象
        student.setStudentId(studentId);
        int checkstatus = studentService.checkstatus(student);// 修改Service方法
        if (checkstatus == 1){
            return Result.success(1);
        }
        return Result.success(0);
    }
    @PutMapping("/userinfo")
    public Result getUserInfo(@RequestBody Student student, HttpServletRequest request){
        // 从request中获取studentId
        Integer studentId = (Integer) request.getAttribute("studentId");
        // 设置studentId到student对象
        student.setStudentId(studentId);
        studentService.UpdateUserInfo(student);
        log.info("修改后的学生：{}",student);
        return Result.success("信息更新成功");
    }

    //小程序展示资讯（只展示已发布的）
    @GetMapping("/information/list")
    public Result informationpage(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult pageBean=studentService.informationpage(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    @GetMapping("/category/{categoryid}")
    public Result<List<Information>> getBycategoryId(@PathVariable Integer categoryid){
        log.info("根据分类id查询资讯,categoryid:{}",categoryid);
        List<Information> informationList = informationService.getcategoryId(categoryid);
        if (informationList!=null){
            return Result.success(informationList);
        }
        return Result.error("未找到该分类下的资讯");
    }

    @GetMapping("/doctor/status/{id}")
    public Result<List<Doctor>> getByDoctorId(@PathVariable Integer id){
        log.info("根据医生状态查询不同类型医生,id:{}",id);
        List<Doctor> doctor = informationService.getstatus(id);
        if (doctor!=null){
            return Result.success(doctor);
        }
        return Result.error("未找到该医生");
    }
    //根据资讯id查看资讯
    @GetMapping("/infoID/{informationId}")
    public Result<Information> getBYTitle(@PathVariable int informationId){
        log.info("根据资讯id查询资讯,id:{}",informationId);
        Information information = informationService.getBYInformationId(informationId);
        if (information !=null){
            return Result.success(information);
        }
        return Result.error("未找到该资讯");
    }

    //预约记录查询
    @GetMapping("/appointment/list")
    public Result appointmentpage(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  int appointment,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询参数：{},{},{},{},{}",page,pageSize,appointment,begin,end);
        PageResult pageBean=doctorService.appointmentpage(page,pageSize,appointment,begin,end);
        return Result.success(pageBean);
    }



    //查询紧急联系人的电话
    @GetMapping("/phone/{id}")
    public Result<String> getPhone(@PathVariable Integer id){
        log.info("根据id查询紧急联系人电话,id:{}",id);
        String phone = studentService.getPhone(id);
        if (phone!=null){
            return Result.success(phone);
        }
        return Result.error("未找到该紧急联系人");
    }
    }
